package com.emagic.manage.data.db.entry;

import android.os.Parcel;
import android.os.Parcelable;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.ToMany;

import java.util.List;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Transient;

/**
 * 巡检、保养记录表
 * Created by xiatian on 2017/8/15.
 */
@Entity
public class Record implements Parcelable {

    public static final int WATTING = 0;        //未上传
    public static final int FINISHED = 1;       //上传完成
    public static final int ERROR = 2;          //异常
    public static final int RUNNING = 3;        //正在执行


    @Id(autoincrement = true)
    private Long id;                   //唯一标示id
    private String rid;                //设备id
    private String planid;             //计划id
    private String uid;                 //用户id
    private String remark;             //描述
    private String makerepair;         //是否报修  1是0否
    private String interfacename;      //巡检或保养  "inspection"巡检  "maintain"保养
    private String subtime;            //记录此数据时间

    private int state;               //状态 0：未上传 1：已上传 2：上传失败

    @Transient
    private String msg;             //离线时提示信息

    @ToMany(referencedJoinProperty = "recordId")
    private List<RecordItem> item;     //保养项or巡检项
    @ToMany(referencedJoinProperty = "recordId")
    private List<RecordPhotos> photos;       //图片
    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;
    /** Used for active entity operations. */
    @Generated(hash = 765166123)
    private transient RecordDao myDao;

    @Generated(hash = 1955077733)
    public Record(Long id, String rid, String planid, String uid, String remark,
            String makerepair, String interfacename, String subtime, int state) {
        this.id = id;
        this.rid = rid;
        this.planid = planid;
        this.uid = uid;
        this.remark = remark;
        this.makerepair = makerepair;
        this.interfacename = interfacename;
        this.subtime = subtime;
        this.state = state;
    }

    @Generated(hash = 477726293)
    public Record() {
    }

    protected Record(Parcel in) {
        id = in.readLong();
        rid = in.readString();
        planid = in.readString();
        uid = in.readString();
        remark = in.readString();
        makerepair = in.readString();
        interfacename = in.readString();
        subtime = in.readString();
        state = in.readInt();
    }

    public static final Creator<Record> CREATOR = new Creator<Record>() {
        @Override
        public Record createFromParcel(Parcel in) {
            return new Record(in);
        }

        @Override
        public Record[] newArray(int size) {
            return new Record[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(rid);
        dest.writeString(planid);
        dest.writeString(uid);
        dest.writeString(remark);
        dest.writeString(makerepair);
        dest.writeString(interfacename);
        dest.writeString(subtime);
        dest.writeInt(state);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRid() {
        return this.rid;
    }

    public void setRid(String rid) {
        this.rid = rid;
    }

    public String getPlanid() {
        return this.planid;
    }

    public void setPlanid(String planid) {
        this.planid = planid;
    }

    public String getUid() {
        return this.uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getRemark() {
        return this.remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getMakerepair() {
        return this.makerepair;
    }

    public void setMakerepair(String makerepair) {
        this.makerepair = makerepair;
    }

    public String getInterfacename() {
        return this.interfacename;
    }

    public void setInterfacename(String interfacename) {
        this.interfacename = interfacename;
    }

    public String getSubtime() {
        return this.subtime;
    }

    public void setSubtime(String subtime) {
        this.subtime = subtime;
    }

    public int getState() {
        return this.state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 1214980397)
    public List<RecordItem> getItem() {
        if (item == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            RecordItemDao targetDao = daoSession.getRecordItemDao();
            List<RecordItem> itemNew = targetDao._queryRecord_Item(id);
            synchronized (this) {
                if (item == null) {
                    item = itemNew;
                }
            }
        }
        return item;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 488856272)
    public synchronized void resetItem() {
        item = null;
    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 1502342509)
    public List<RecordPhotos> getPhotos() {
        if (photos == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            RecordPhotosDao targetDao = daoSession.getRecordPhotosDao();
            List<RecordPhotos> photosNew = targetDao._queryRecord_Photos(id);
            synchronized (this) {
                if (photos == null) {
                    photos = photosNew;
                }
            }
        }
        return photos;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 781103891)
    public synchronized void resetPhotos() {
        photos = null;
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 1505145191)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getRecordDao() : null;
    }
}
